package com.example.zhanghao.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zhanghao.DTO.OrderDto;
import com.example.zhanghao.DTO.PayDto;
import com.example.zhanghao.DTO.PayordersDto;
import com.example.zhanghao.Entity.Orders;
import com.example.zhanghao.Entity.Payorders;
import com.example.zhanghao.Entity.User;
import com.example.zhanghao.VO.CommonVo;
import com.example.zhanghao.VO.OrderVo;
import com.example.zhanghao.VO.PageVo;
import com.example.zhanghao.services.impl.PayordersServiceImpl;
import com.example.zhanghao.services.impl.UserServiceImpl;
import com.example.zhanghao.utils.CopyUtil;
import com.example.zhanghao.utils.HupijiaoHandler;
import com.example.zhanghao.utils.SnowFlake;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController //这是返回接口
@RequestMapping("/payOrders")
public class PayOrdersController {

    private static final Logger LOG = LoggerFactory.getLogger(PayOrdersController.class);

    @Resource
    private SnowFlake snowFlake;

    @Resource
    private PayordersServiceImpl payordersService ;

    @Resource
    private RedisTemplate redisTemplate;
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private UserServiceImpl userServiceImpl;

    @PostMapping("/add")
    public CommonVo<Payorders> add(@Valid @RequestBody PayordersDto req) {
        Payorders payorders = CopyUtil.copy(req, Payorders.class);
        payordersService.save(payorders);
        CommonVo<Payorders> commonVo = new CommonVo();
        commonVo.setData(payorders);
        return commonVo;
    }

    @GetMapping("/findOrder")
    public CommonVo<User> findOrder( HttpServletRequest request,@Valid Payorders req) {
        LambdaQueryWrapper<Payorders> PayordersLambdaQueryWrapper = new LambdaQueryWrapper();
        PayordersLambdaQueryWrapper.eq(req.getTradeOrderId() != null,Payorders::getTradeOrderId,req.getTradeOrderId());
        PayordersLambdaQueryWrapper.eq(req.getUserId() != null,Payorders::getUserId,req.getUserId());
        PayordersLambdaQueryWrapper.eq(req.getStatus() != null,Payorders::getStatus,req.getStatus());
        Payorders payorders = payordersService.getOne(PayordersLambdaQueryWrapper);

        CommonVo<User> commonVo = new CommonVo<>();

        LambdaQueryWrapper<User> UserLambdaQueryWrapper = new LambdaQueryWrapper();
        UserLambdaQueryWrapper.eq(req.getUserId() != null,User::getId,req.getUserId());
        User user = userServiceImpl.getOne(UserLambdaQueryWrapper);

        if(payorders!=null){
            commonVo.setData(user);
        }

        return commonVo;
    }

    @PostMapping("/changeStatus")
    public String changeStatus( @Valid @RequestParam String trade_order_id, @Valid @RequestParam String status) {
        System.out.println(trade_order_id);
        System.out.println(status);
        if(status.equals("OD")){
            LambdaQueryWrapper<Payorders> PayordersLambdaQueryWrapper = new LambdaQueryWrapper();
            PayordersLambdaQueryWrapper.eq(trade_order_id != null,Payorders::getTradeOrderId,trade_order_id);
            Payorders payorders = payordersService.getOne(PayordersLambdaQueryWrapper);

            if(payorders!=null){

                payorders.setStatus("1");
                payordersService.updateById(payorders);

                LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper();
                userLambdaQueryWrapper.eq(payorders.getUserId() != null,User::getId,payorders.getUserId());
                User user = userServiceImpl.getOne(userLambdaQueryWrapper);
                Integer totalFee = Integer.parseInt(payorders.getTotalFee());
                user.setMoney(user.getMoney()+totalFee);

                userServiceImpl.updateById(user);

                return "success";
            }else{
                return "fail";
            }
        }else {
            return "fail";
        }
    }

}

